package com.seeyon.apps.brwh.manager.impl;

import com.seeyon.apps.brwh.kit.DBKit;
import com.seeyon.apps.brwh.kit.FormCap4Kit;
import com.seeyon.apps.brwh.manager.SupplierCooperationService;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.cap4.template.manager.CAPDataListSqlHelpManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.common.po.ctpenumnew.CtpEnumItem;
import org.apache.commons.logging.Log;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商雷达图接口业务实现类
 * @author ys
 * @create 2024-07-06-13:58
 */
public class SupplierCooperationServiceImpl implements SupplierCooperationService {

    private static final Log log = CtpLogFactory.getLog(SupplierCooperationServiceImpl.class);
    private CAP4FormManager cap4FormManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");

    private CAPDataListSqlHelpManager capDataListSqlHelpManager = (CAPDataListSqlHelpManager) AppContext.getBean("capDataListSqlHelpManager");

    /*获取供应商雷达图数据*/
    @Override
    public  Map<String, Object> getFineSumData() {
        Map<String, Object> resultMap = new HashMap<>();

        Map<String, Object> res = new HashMap<>();
        String url = FormCap4Kit.getBbAddres(AppContext.getSystemProperty("brwh.gycfjl"));
        try {
            String gysTempCode = AppContext.getSystemProperty("brwh.gysdaTemp");
            FormBean formBean = cap4FormManager.getFormByFormCode(gysTempCode);
            String tableName = formBean.getFieldBeanByDisplay("累计处罚罚金").getOwnerTableName();
            //            累计处罚罚金
            String fineSum=formBean.getFieldBeanByDisplay("累计处罚罚金").getName();
            //获取枚举编码
            List<CtpEnumItem> gysdjZs = FormCap4Kit.getEnumByNameList("gysdj_zs");
            if (gysdjZs == null || gysdjZs.isEmpty()){
                res.put("code", 500);
                res.put("msg", "枚举值编码为空");
                res.put("data", "");
                return res;
            }
            Long enumId = gysdjZs.get(0).getId();
            //    供应商等级
            String supplierGrade=formBean.getFieldBeanByDisplay("供应商等级").getName();

            String sql = "select SUM("+fineSum+") as finesum from "+tableName+" where "+supplierGrade+" = '"+enumId+"'";
            log.info("供应商雷达图主表中标率sql语句：" + sql);
            List list = capDataListSqlHelpManager.selectDataList(sql, null);
            if (list != null && list.size() >0){
                log.info("list：" + list.get(0));
                Map<String, Object> dataMap = (Map<String, Object>) list.get(0);
                double fineCount=0;
                String  intermediateVariable="0";
                if( dataMap.get( "finesum") != null){
                    DecimalFormat df = new DecimalFormat("0.0");
                    fineCount = Double.parseDouble( dataMap.get( "finesum").toString());
                    intermediateVariable=df.format(fineCount);
                }

                resultMap.put("fineSum", intermediateVariable);

                resultMap.put("url", url);
                res.put("data", resultMap);
                res.put("code", 200);
                res.put("msg", "成功");
                return res;
            }else {
                resultMap.put("fineSum", 0);
                resultMap.put("url", url);
                res.put("data", "");
                res.put("code", 200);
                res.put("msg", "成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            res.put("code", 500);
            res.put("msg", e.getMessage());
            res.put("data", "");
            return res;
        }
        res.put("code", 200);
        res.put("msg", "成功");
        res.put("data", "");
        return res;
    }

    /*获取雷达图累计罚款数据*/
    @Override
    public Map<String, Object> getSupplierCooperationList() {
        Map<String, Object> fakeData = new HashMap<>();
        Map<String, Object> fakeMapa = new HashMap<>();
        Map<String, Object> res = new HashMap<>();

        try {
            String gysTempCode = AppContext.getSystemProperty("brwh.gysdaTemp");
            String gysjxpg = AppContext.getSystemProperty("brwh.gysjxpg");
//            为空预制数据

            String url = FormCap4Kit.getBbAddres(gysjxpg);
            fakeMapa.put("technologicalInnovationLevel",0);
            fakeMapa.put("qualityPassRate", 0);
            fakeMapa.put("timelyDeliveryRate", 0);
            fakeMapa.put("serviceLevel", 0);
            fakeMapa.put("priceRationality", 0);
            fakeMapa.put("url", url);
            fakeData.put("data", fakeMapa);
            fakeData.put("code", 200);
            fakeData.put("msg", "成功 ");

            FormBean formBean = cap4FormManager.getFormByFormCode(gysTempCode);
            String rLevel = formBean.getFieldBeanByDisplay("供应商等级").getName();
            String tableName = formBean.getFieldBeanByDisplay("累计处罚罚金").getOwnerTableName();
            //            累计处罚罚金
            String fineSum=formBean.getFieldBeanByDisplay("累计处罚罚金").getName();
            //获取枚举编码
            List<CtpEnumItem> gysdjZs = FormCap4Kit.getEnumByNameList("gysdj_zs");
            if (gysdjZs == null || gysdjZs.isEmpty()){
                res.put("code", 500);
                res.put("msg", "枚举值编码为空");
                res.put("data", "");
                return res;
            }
            Long enumId = gysdjZs.get(0).getId();

            //    供应商等级
            String supplierGrade=formBean.getFieldBeanByDisplay("供应商等级").getName();

            //技术创新率
            String technologicalInnovationLevel = formBean.getFieldBeanByDisplay("得分一最新得分").getName();
            //质量合格率
            String qualityPassRate = formBean.getFieldBeanByDisplay("得分二最新得分").getName();
            //交货及时率
            String timelyDeliveryRate = formBean.getFieldBeanByDisplay("得分四最新得分").getName();
            //售后服务水平
            String serviceLevel = formBean.getFieldBeanByDisplay("得分三最新得分").getName();
            //价格合理
            String priceRationality = formBean.getFieldBeanByDisplay("得分五最新得分").getName();

            String countSql = "SELECT COUNT(*) as count FROM " + tableName + " where "+rLevel+" = '"+enumId+"'";
            String datasql = "select SUM(" + technologicalInnovationLevel + ") as sumtech,SUM(" + qualityPassRate + ") as sumquality,SUM(" + timelyDeliveryRate + ") as sumtimely,SUM(" + serviceLevel + ") as sumservice,SUM(" + priceRationality + ") as sumprice from " + tableName + " where "+rLevel+" = '"+enumId+"'";

            log.info("供应商雷达图主表中标率sql语句1：" + datasql);
            log.info("供应商雷达图主表中标率sql语句2：" + countSql);
            List dataList = capDataListSqlHelpManager.selectDataList(datasql, null);
            List countList = capDataListSqlHelpManager.selectDataList(countSql, null);
            double count=0;
            double tl=0;
            double qpr=0;
            double tdr=0;
            double sl=0;
            double pr=0;
            if (dataList != null && dataList.size() == 1 && countList != null && countList.size() == 1){
//                处理数据为空的情况
                /*处理查询数量·*/
                if(((Map<String, Object>) countList.get(0)).get("count")!=null){
                     count = Double.parseDouble(((Map<String, Object>) countList.get(0)).get("count").toString());
                }
                /*处理技术创新率*/
                if( ((Map<String, Object>) dataList.get(0)).get("sumtech")!=null){
                    tl = Double.parseDouble(((Map<String, Object>) dataList.get(0)).get("sumtech").toString());
                }
                /*处理质量合格率*/
                if (((Map<String, Object>) dataList.get(0)).get("sumquality")!=null){
                    qpr = Double.parseDouble(((Map<String, Object>) dataList.get(0)).get("sumquality").toString());
                }
                /*处理交货及时率*/
                if ( ((Map<String, Object>) dataList.get(0)).get("sumtimely")!=null){
                    tdr = Double.parseDouble(((Map<String, Object>) dataList.get(0)).get("sumtimely").toString());
                }
                /*处理售后服务水平*/
                if ( ((Map<String, Object>) dataList.get(0)).get("sumservice")!=null){
                    sl = Double.parseDouble(((Map<String, Object>) dataList.get(0)).get("sumservice").toString());
                }
                /*处理价格合理率*/
                if (((Map<String, Object>) dataList.get(0)).get("sumprice")!=null){
                    pr = Double.parseDouble(((Map<String, Object>) dataList.get(0)).get("sumprice").toString());
                }

                Map<String, Object> resultMap = new HashMap<>();
                if (count == 0){
                    return fakeData;
                }

                /*运算结果保留两位小数*/
                DecimalFormat df = new DecimalFormat("#.0");
                tl = Double.parseDouble(df.format(tl/count));
                qpr = Double.parseDouble(df.format(qpr/count));
                tdr = Double.parseDouble(df.format(tdr/count));
                sl = Double.parseDouble(df.format(sl/count));
                pr = Double.parseDouble(df.format(pr/count));
                /*放入结果map*/
                resultMap.put("technologicalInnovationLevel",tl);
                resultMap.put("qualityPassRate", qpr);
                resultMap.put("timelyDeliveryRate",tdr);
                resultMap.put("serviceLevel", sl);
                resultMap.put("priceRationality",pr);
                resultMap.put("url", url);
                res.put("data", resultMap);
                res.put("code", 200);
                res.put("msg", "成功");
                return res;
            }else {
                return fakeData;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            res.put("code", 500);
            res.put("msg", e.getMessage());
            res.put("data", "");
            return res;
        }

    }
}
